<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/8 0008
 * Time: 13:44
 */

namespace app\index\model;


use think\Model;

class Order extends Model
{
    //商品名order_sn模糊查询
    public function searchOrderSnAttr($query,$value,$data){
        if(!empty($value)){
            $query->whereLike('order_sn','%'.$value.'%');
        }
    }
    // 订单状态
    public function searchOrderStatusAttr($query,$value){
        //针对复选框形式
        if(is_array($value) && count($value) > 0 ){
            $query->whereIn('status',$value);
        }
    }

    // 价格区间
    public function searchPriceMinAttr($query,$value){
        if(''!==$value){
            $query->where('order_amount','>=',$value);
        }
    }

    public function searchPriceMaxAttr($query,$value){
        if(''!==$value){
            $query->where('order_amount','<=',$value);
        }
    }


    public static function getOrder($data = [],$query = [])
    {
        $field=[
            'order_id','buyer_name',
            'o.status'=>'ostatus','goods_amount','status_id','order_status',
            'FROM_UNIXTIME(add_time,\'%Y-%m-%d %H:%i:%S\')' => 'add_date',
            'postscript','order_sn','order_amount',
        ];
        $model = self::alias('o')
            ->field($field)
            ->join(['ecm_order_status'=>'s'],'o.status=s.status_id','LEFT')
            ->withSearch(array_keys($data),$data);
//        //商品名order_sn模糊查询
//        if (isset($data['order_sn']) && '' !== $data['order_sn']){
//            $model->whereLike('order_sn','%'.$data['order_sn'].'%');
//        }
//        //订单状态复选框查询
//        if(isset($data['order_status']) && is_array($data['order_status']) && count($data['order_status']) > 0){
//            $model->whereIn('o.status',$data['order_status']);
//        }
//        //价格区间
//        if (isset($data['min_price']) && is_array($data['min_price'])){
//            $model->whereIn('price','>=',$data['min_price']);
//        }
//        if (isset($data['max_price']) && is_array($data['max_price'])){
//            $model->whereIn('price','<=',$data['max_price']);
//        }
        // 分页
        $result = $model->paginate([
            'list_rows' => 10,
            'query'     => $query,
        ])->each(function ($item) use ($data) {
            if (isset($data['order_sn']) && '' != $data['order_sn']) {
                $item['order_sn'] =
                    str_replace($data['order_sn'],
                        '<span style="color: red;font-weight: 900">' . $data['order_sn'] . '</span>', $item['order_sn']);
                return $item;
            }
        });

        return $result;
    }
}